import type { DynamicFormComponentInst } from '@/components/DynamicFormComponent/index.vue'
import { ref } from 'vue'

export const defaultVersionTypeKey = 'BMCOM003'
export const defaultVersionTypeValue = 'T001001'

export default function useSubmit(onSubmit?: () => void) {
  const dfInst = ref<DynamicFormComponentInst | null>(null)
  const formData = ref({})
  const createVars = () => ({ formData: formData.value })
  const submitLoading = ref(false)

  const submit = async() => {
    try {
      await dfInst.value?.formInst?.validate()
      onSubmit?.()
      return true
    } catch (error) {
      console.error(error)
      return false
    }
  }

  return {
    dfInst,
    formData,
    createVars,
    submitLoading,
    submit
  }
}
